import {
    HOME_SET_DATA,
    DISCOVER_SET_DATA,
    CATEGORIES_SET_DATA,
    DAILY_SET_DATA,
    REC_SET_DATA,
    VIDEO_SET_DATA,
    DETAIL_SET_DATA,
    DTREC_SET_DATA,
    SQUARE_SET_DATA,
    REGISTER_SET_DATA,
    LOGIN_SET_DATA,
    USER_SET_DATA,
    THEME_SET_DATA,
    THEME_REMOVE_DATA,
    PGC_REMOVE_DATA,
    PGC_SET_DATA,
    LIKE_SET_DATA,
    LIKE_REMOVE_DATA,
    WORKS_SET_DATA,
    WORKS_SET_LIKE,
    WORKS_REMOVE_LIKE,
    ADD_COMMENT_DATA,
    COMMENT_DATA,
    ADD_REPLY_DATA,
    USERPGC_SET_DATA,
    PGC_DATA,
    PGC_AUTHOR_DATA,
    THEME_VIDEOS_DATA,
    USERPGC_DATA,
    USERAUTHOR_DATA,
    USERTHEME_DATA,
    USERVIDEOS_DATA,
    PUSH_DATA,
    USERCOMMENT_DATA,
    WORKLIKE_DATA,
    EMPTYUSERLIKE,
    EMPTYWORKSLIKE,
    GETAYTHORDATA,
    GETAUTHORVIDEODATA,
    GETWEEKRANK,
    GETHISTORYRANK,
    GETMONTHRANK
} from './actionType';
import Axios from 'axios';
// Axios.defaults.baseURL = "http://59.110.237.148:6602/";
Axios.defaults.baseURL = "http://baobab.kaiyanapp.com/";

export let setHome = obj => ({
    type: HOME_SET_DATA,
    obj
})

export let getHome = () => dispatch => {
    Axios.get("/api/v5/index/tab/allRec", {
        params: {
            page: 0
        }
    }).then(({
        data
    }) => {
        dispatch(setHome(data));
    });
}

export let setDiscover = obj => ({
    type: DISCOVER_SET_DATA,
    obj
})

export let getDiscover = () => dispatch => {
    Axios.get("/api/v7/index/tab/discovery").then(({
        data
    }) => {
        dispatch(setDiscover(data));
    });
}

export let setCategories = obj => ({
    type: CATEGORIES_SET_DATA,
    obj
})

export let getCategories = () => dispatch => {
    Axios.get("/api/v4/categories/all").then(({
        data
    }) => {
        dispatch(setCategories(data));
    });
}

export let setDaily = obj => ({
    type: DAILY_SET_DATA,
    obj
})

export let getDaily = () => dispatch => {
    Axios.get("/api/v5/index/tab/feed").then(({
        data
    }) => {
        dispatch(setDaily(data));
    });
}

export let setRec = obj => ({
    type: REC_SET_DATA,
    obj
})

export let getRec = () => dispatch => {
    Axios.get("/api/v7/community/tab/rec").then(({
        data
    }) => {
        dispatch(setRec(data));
    });
}

export let setVideo = obj => ({
    type: VIDEO_SET_DATA,
    obj
})

export let setWorks = data => ({
    type: WORKS_SET_DATA,
    data
})

export let getVideo = (vid) => dispatch => {
    Axios.get("/api/v2/video/" + vid).then(({
        data
    }) => {
        Axios.post("http://59.110.237.148:6602/ecs/works", {
            vid: data.id,
            aid: data.author.id,
            vlike: [],
            vcommit: [],
            state: 1,
            title: data.title,
            category: data.category,
            detailImg: data.cover.detail,
            duration: data.duration
        }).then(({
            data
        }) => {
            dispatch(setWorks(data))
        });
        dispatch(setVideo(data));
    });
}

export let getWorkLike = (ulike, cb) => {
    return Axios.post("http://59.110.237.148:6602/ecs/workLike", { ulike }).then(({ data }) => {
        cb(data)
        return {
            type: WORKLIKE_DATA,
            data
        }
    })
}

export let setDetail = obj => ({
    type: DETAIL_SET_DATA,
    obj
})

export let getDetail = (did) => dispatch => {
    Axios.get("/api/v6/tag/index", {
        params: {
            id: did
        }
    }).then(({
        data
    }) => {
        dispatch(setDetail(data));
    });
}

export let setDtrec = obj => ({
    type: DTREC_SET_DATA,
    obj
})

export let getDtrec = (id) => dispatch => {
    Axios.get("/api/v1/tag/videos", {
        params: {
            id
        }
    }).then(({
        data
    }) => {
        dispatch(setDtrec(data));
    });
}

export let getSquare = (id) => {
    return Axios.get("/api/v6/tag/dynamics", {
        params: {
            id
        }
    }).then(({
        data
    }) => {
        return {
            type: SQUARE_SET_DATA,
            data
        }
    });
}

export let getRegister = (tel, pwd, cb) => {
    return Axios.post("http://59.110.237.148:6602/ecs/users/register", {
        utel: tel,
        upwd: pwd,
        state: 1,
        uatt_theme: [],
        uatt_pgc: [],
        ulike: [],
    }).then(({
        data
    }) => {
        cb(data);
        return {
            type: REGISTER_SET_DATA,
            data
        }
    });
}

export let getLogin = (tel, pwd, cb) => {
    return Axios.post("http://59.110.237.148:6602/ecs/users/login", {
        utel: tel,
        upwd: pwd,
        state: 1
    }).then(({
        data
    }) => {
        cb(data);
        return {
            type: LOGIN_SET_DATA,
            data
        }
    });
}

export let getUser = (id, cb) => {
    if (id) {
        return Axios.post("http://59.110.237.148:6602/ecs/users/user", {
            _id: id,
            state: 1
        }).then(({
            data
        }) => {
            cb(data);
            return {
                type: USER_SET_DATA,
                data
            }
        });
    } else {
        return {
            type: USER_SET_DATA,
            data: null
        }
    }
}

export let setTheme = (uid, id, cb) => {
    return Axios.post("http://59.110.237.148:6602/ecs/users/theme", {
        uatt_theme: id,
        _id: uid
    }).then(({
        data
    }) => {
        cb(data);
        return {
            type: THEME_SET_DATA,
            data
        }
    });
}

export let removeTheme = (uid, id, cb) => {
    return Axios.post("http://59.110.237.148:6602/ecs/users/remove-theme", {
        uatt_theme: id,
        _id: uid
    }).then(({
        data
    }) => {
        cb(data);
        return {
            type: THEME_REMOVE_DATA,
            data
        }
    });
}

export let setPgc = (uid, id, cb) => {
    return Axios.post("http://59.110.237.148:6602/ecs/users/pgc", {
        uatt_pgc: id,
        _id: uid
    }).then(({
        data
    }) => {
        cb(data);
        return {
            type: PGC_SET_DATA,
            data
        }
    });
}

export let setAuthorAndPgc = (id) => {
    Axios.get("/api/v5/userInfo/tab?id=" + id + "&userType=PGC").then(({ data }) => {
        data.vid = id;
        Axios.post("http://59.110.237.148:6602/ecs/author", data).then(({ data }) => {
        })
    })
    Axios.get("/api/v5/userInfo/tab/index?id=" + id + "&userType=PGC").then(({ data }) => {
        data.vid = id;
        Axios.post("http://59.110.237.148:6602/ecs/pgcList", data).then(({ data }) => {
        })
    })
    return {
        type: PGC_AUTHOR_DATA,
        data: null
    }
}

export let setThemeAndVideos = (id) => {
    Axios.get("/api/v6/tag/index", { params: { id } }).then(({ data }) => {
        data.vid = id;
        Axios.post("http://59.110.237.148:6602/ecs/theme", data).then(({ data }) => {
        })
    })
    Axios.get("/api/v1/tag/videos", { params: { id } }).then(({ data }) => {
        data.vid = id;
        Axios.post("http://59.110.237.148:6602/ecs/videos", data).then(({ data }) => {
        })
    })
    return {
        type: THEME_VIDEOS_DATA,
        data: null
    }
}

export let removePgc = (uid, id, cb) => {
    return Axios.post("http://59.110.237.148:6602/ecs/users/remove-pgc", {
        uatt_pgc: id,
        _id: uid
    }).then(({
        data
    }) => {
        cb(data);
        return {
            type: PGC_REMOVE_DATA,
            data
        }
    });
}

export let setLike = (uid, id, cb) => {
    return Axios.post("http://59.110.237.148:6602/ecs/users/like", {
        ulike: id,
        _id: uid
    }).then(({
        data
    }) => {
        cb(data);
        return {
            type: LIKE_SET_DATA,
            data
        }
    });
}

export let removeLike = (uid, id, cb) => {
    return Axios.post("http://59.110.237.148:6602/ecs/users/remove-like", {
        ulike: id,
        _id: uid
    }).then(({
        data
    }) => {
        cb(data);
        return {
            type: LIKE_REMOVE_DATA,
            data
        }
    });
}

export let setWorksLike = (uid, vid, cb) => {
    return Axios.post("http://59.110.237.148:6602/ecs/works-like", {
        vlike: uid,
        vid: vid
    }).then(({
        data
    }) => {
        cb(data);
        return {
            type: WORKS_SET_LIKE,
            data
        }
    });
}

export let removeWorksLike = (uid, vid, cb) => {
    return Axios.post("http://59.110.237.148:6602/ecs/remove-works-like", {
        vlike: uid,
        vid: vid
    }).then(({
        data
    }) => {
        cb(data);
        return {
            type: WORKS_REMOVE_LIKE,
            data
        }
    });
}

export let setComment = (content, vid, uid, utel, cb) => {
    return Axios.post("http://59.110.237.148:6602/ecs/addComment", {
        content,
        vid,
        uid,
        utel,
        state: 1,
        replyList: []
    }).then(({
        data
    }) => {
        cb(data);
        return {
            type: ADD_COMMENT_DATA,
            data
        }
    });
}

export let getCommentList = (vid, cb) => {
    return Axios.get("http://59.110.237.148:6602/ecs/commentList", {
        params: {
            vid
        }
    }).then(({
        data
    }) => {
        cb(data);
        return {
            type: COMMENT_DATA,
            data
        }
    });
}

export let setReply = (content, vid, uid, utel, _id, cb) => {
    return Axios.post("http://59.110.237.148:6602/ecs/addReply", {
        content,
        vid,
        uid,
        utel,
        state: 1,
        replyList: [],
        replyId: _id
    }).then(({
        data
    }) => {
        cb(data);
        return {
            type: ADD_REPLY_DATA,
            data
        }
    });
}

export let getUserPgc = (uatt_pgc, cb) => {
    if (uatt_pgc.length !== 0) {
        return Axios.post("http://59.110.237.148:6602/ecs/getAttPgc", { uatt_pgc }).then(({ data }) => {
            cb(data)
            return {
                type: USERPGC_DATA,
                data
            }
        })
    } else {
        return {
            type: USERPGC_DATA,
            data: null
        }
    }
}

export let getUserAuthor = (uatt_pgc, cb) => {
    if (uatt_pgc.length !== 0) {
        return Axios.post("http://59.110.237.148:6602/ecs/getAuthor", { uatt_pgc }).then(({ data }) => {
            cb(data)
            return {
                type: USERAUTHOR_DATA,
                data
            }
        })
    } else {
        return {
            type: USERAUTHOR_DATA,
            data: null
        }
    }
}

export let getUserTheme = (uatt_theme, cb) => {
    if (uatt_theme.length !== 0) {
        return Axios.post("http://59.110.237.148:6602/ecs/getTheme", { uatt_theme }).then(({ data }) => {
            cb(data)
            return {
                type: USERTHEME_DATA,
                data
            }
        })
    } else {
        return {
            type: USERTHEME_DATA,
            data: null
        }
    }
}

export let getUserVideos = (uatt_theme, cb) => {
    if (uatt_theme.length !== 0) {
        return Axios.post("http://59.110.237.148:6602/ecs/getVideos", { uatt_theme }).then(({ data }) => {
            cb(data)
            return {
                type: USERVIDEOS_DATA,
                data
            }
        })
    } else {
        return {
            type: USERVIDEOS_DATA,
            data: null
        }
    }
}

export let setPgcData = (data) => {
    return {
        type: PGC_DATA,
        data
    }
}

export let getPush = () => {
    return Axios.get("/api/v3/messages").then(({ data }) => {
        return {
            type: PUSH_DATA,
            data
        }
    })
}

export let getUserComment = (uid) => {
    return Axios.post("http://59.110.237.148:6602/ecs/getUserComment", { uid }).then(({ data }) => {
        return {
            type: USERCOMMENT_DATA,
            data
        }
    })
}

export let emptyUserLike = (uid, cb) => {
    return Axios.post("http://59.110.237.148:6602/ecs/emptyLike", { uid }).then(({ data }) => {
        cb(data)
        return {
            type: EMPTYUSERLIKE,
            data
        }
    })
}

export let emptyWorksLike = (uid, cb) => {
    return Axios.post("http://59.110.237.148:6602/ecs/emptyWorksLike", { uid }).then(({ data }) => {
        cb(data)
        return {
            type: EMPTYWORKSLIKE,
            data
        }
    })
}

export let getAuthorData = (id, cb) => {
    return Axios.get("/api/v5/userInfo/tab?id=" + id + "&userType=PGC").then(({ data }) => {
        cb(data)
        return {
            type: GETAYTHORDATA,
            data
        }
    })
}

export let getAuthorVideoData = (id, cb) => {
    return Axios.get("/api/v5/userInfo/tab/index?id=" + id + "&userType=PGC").then(({ data }) => {
        cb(data)
        return {
            type: GETAUTHORVIDEODATA,
            data
        }
    })
}

export let getWeekRank = () => {
    return Axios.get("/api/v4/rankList/videos?strategy=weekly").then(({ data }) => {
        return {
            type: GETWEEKRANK,
            data
        }
    })
}

export let getMonthRank = () => {
    return Axios.get("/api/v4/rankList/videos?strategy=monthly").then(({ data }) => {
        return {
            type: GETMONTHRANK,
            data
        }
    })
}

export let getHistoryRank = () => {
    return Axios.get("/api/v4/rankList/videos?strategy=historical").then(({ data }) => {
        return {
            type: GETHISTORYRANK,
            data
        }
    })
}